let fs=require('fs')
let path=require('path')

let express=require('express')

let app=express();


app.use(express.static('./public'))

app.use(express.json());

app.get('/',(req,res)=>{
    res.end(fs.writeFileSync(path.join(__dirname, './public/index.html')))
})
app.get('/a',(req,res)=>{
    res.end(fs.writeFileSync(path.join(__dirname, './data.json')))
})
app.get('/new',(req,res)=>{
    console.log(req.body);
    
    // 读取json文件
   let arr=JSON.parse(fs.readFileSync('./data.json'))
   arr.push(req.body)
//    写入
    fs.writeFileSync('./data.json',JSON.stringify(arr))
    res.json({
        code:1,
        mgs:"添加成功"  
    })

})
app.listen(8080,()=>{
    console.log('启动服务器 http://localhost:8080/');
})